<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>添加员工信息</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link rel="stylesheet" type="text/css"
			href="css/style.css" />
	</head>

	<body>
		<div id="wrap">
			<div id="top_content">
					<div id="header">
						<div id="rightheader">
							<p>
								<span v-text="date"></span>
								<br />
								<a href="javascript:;" @click="logout">安全退出</a>
							</p>
						</div>
						<div id="topheader">
							<h1 id="title">
								<a href="/ems_vue/emplist.html">返回首页</a>
							</h1>
						</div>
						<div id="navigation">
						</div>
					</div>
				<div id="content">
					<p id="whereami">
					</p>
					<h1>
						添加员工信息:
					</h1>
					<form action="emplist.html" method="post">
						<table cellpadding="0" cellspacing="0" border="0"
							class="form_table">
							<tr>
								<td valign="middle" align="right">
									姓名:
								</td>
								<td valign="middle" align="left">
									<input type="text" class="inputgri" v-model="emp.name" name="name" />
								</td>
							</tr>
							<tr>
								<td valign="middle" align="right">
									头像:
								</td>
								<td valign="middle" align="left">
									<input type="file" ref="photo" width="" name="photo" />
								</td>
							</tr>
							<tr>
								<td valign="middle" align="right">
									工资:
								</td>
								<td valign="middle" align="left">
									<input type="text" class="inputgri" v-model="emp.salary" name="salary" />
								</td>
							</tr>
							<tr>
								<td valign="middle" align="right">
									年龄:
								</td>
								<td valign="middle" align="left">
									<input type="text" class="inputgri" v-model="emp.age" name="age" />
								</td>
							</tr>
						</table>
						<p>
							<input type="button" @click="saveEmp" class="button" value="确认添加" />
							<input type="button" @click="editAdd" class="button" value="返回列表" />
						</p>
					</form>
				</div>
			</div>
			<div id="footer">
				<div id="footer_bg">
					ranlen@cl.com
				</div>
			</div>
		</div>
	</body>
</html>
<script src="js/vue.js"></script>
<script src="js/axios.min.js"></script>
<script>
	var app = new Vue({
		el:"#wrap",
		data:{
			user:{
				realname:""
			},
			emp:{},
			date:"2022/6"
		},
		methods:{
			logout(){
				//localStorage.removeItem("user");
				axios.get("http://localhost:8989/ems_vue/user/logout").then(res=>{});
				sessionStorage.removeItem("token");
				//location.reload(true);
				location.href = "/ems_vue/login.html";
			},
			saveEmp(){
				console.log(this.emp);
				console.log(this.$refs.photo.files[0]);
				let formData = new FormData();
				formData.append("name",this.emp.name);
				formData.append("salary",this.emp.salary);
				formData.append("age",this.emp.age);
				formData.append("photo",this.$refs.photo.files[0]);
				let _this = this;
				axios({
					method:"post",
					url:"http://localhost:8989/ems_vue/emp/save",
					data:formData,
					headers:{
						'content-type':'multipart/form-data'
					}
				}).then(res=>{
					console.log(res.data);
					if (res.data.state){
						if (window.confirm(res.data.msg+",点击确定跳转列表页面！")){
							location.href = "/ems_vue/emplist.html";
						}else {
							_this.emp = {};
						}
					}else {
						alert(res.data.msg)
					}
				})
			},
			editAdd(){
				location.href = "/ems_vue/emplist.html";
			}
		},
		created(){
			// let u =localStorage.getItem("user");
			// if (u){
			// 	this.user = JSON.parse(u);
			// }else {
			// 	location.href = "/ems_vue/login.html";
			// }
			let aData = new Date();
			this.date =aData.getFullYear() + "/" + (aData.getMonth() + 1) + "/" + aData.getDate();
			let _this = this;
			axios.get("http://localhost:8989/ems_vue/user/getUser").then(res=>{
				if (!res.data.state){
					location.href = "/ems_vue/login.html";
				}
				_this.user = res.data.user;
			})
			let date = new Date();
			this.date = date.toLocaleDateString();
		}
	})
</script>